package flc.ast.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DebugAdapter;
import flc.ast.bean.MyDebugBean;
import flc.ast.databinding.ActivityDebugBinding;
import flc.ast.dialog.DeleteDialog;
import flc.ast.utils.AppSpUtils;

public class DebugActivity extends BaseAc<ActivityDebugBinding> {

    public static int type;
    private DebugAdapter debugAdapter;
    private List<MyDebugBean> listShow = new ArrayList<>();
    private int selPosition;
    private boolean isEdit;

    @Override
    protected int onCreate() {
        return R.layout.activity_debug;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDebugBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvAdd.setOnClickListener(this);
        mDataBinding.tvAddConn.setOnClickListener(this);
        mDataBinding.tvDebugEdit.setOnClickListener(this);
        mDataBinding.tvDebugComplete.setOnClickListener(this);

        mDataBinding.rvDebugList.setLayoutManager(new LinearLayoutManager(mContext));
        debugAdapter = new DebugAdapter();
        mDataBinding.rvDebugList.setAdapter(debugAdapter);
        debugAdapter.addChildClickViewIds(R.id.ivDebugItemDelete);
        debugAdapter.setOnItemChildClickListener(this);
        debugAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvDebugEdit) {//编辑
            if (listShow == null || listShow.size() == 0) return;
            isEdit = true;
            debugAdapter.setEdit(isEdit);
            debugAdapter.notifyDataSetChanged();
            mDataBinding.tvDebugEdit.setVisibility(View.GONE);
            mDataBinding.tvDebugComplete.setVisibility(View.VISIBLE);
        } else if (id == R.id.tvDebugComplete) {//完成
            cancelEdit();
        } else if (id == R.id.tvAdd || id == R.id.tvAddConn) {//添加
            AddDebugActivity.type = type;
            startActivity(AddDebugActivity.class);
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            TcpServerActivity.debugBean = debugAdapter.getItem(position);
            startActivity(TcpServerActivity.class);
        } else if (type == 1) {
            TcpClientActivity.debugBean = debugAdapter.getItem(position);
            startActivity(TcpClientActivity.class);
        }else if (type == 2) {
            UdpServerActivity.debugBean = debugAdapter.getItem(position);
            startActivity(UdpServerActivity.class);
        }else if (type == 3) {
            UdpClientActivity.debugBean = debugAdapter.getItem(position);
            startActivity(UdpClientActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivDebugItemDelete://删除
                selPosition = position;
                showDeleteDialog();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (type == 0) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.tcp_server));
            getTcpServerData();
        } else if (type == 1) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.tcp_client));
            getTcpClientData();
        } else if (type == 2) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.udp_server));
            getUdpServerData();
        } else if (type == 3) {
            mDataBinding.tvDebugTitle.setText(getString(R.string.udp_client));
            getUdpClientData();
        }
    }

    //删除
    private void delete() {
        listShow.remove(selPosition);
        if (type == 0) {
            AppSpUtils.saveTcpServerList(listShow);
            getTcpServerData();
        } else if (type == 1) {
            AppSpUtils.saveTcpClientList(listShow);
            getTcpClientData();
        } else if (type == 2) {
            AppSpUtils.saveUdpServerList(listShow);
            getUdpServerData();
        } else if (type == 3) {
            AppSpUtils.saveUdpClientList(listShow);
            getUdpClientData();
        }
        ToastUtils.showShort(getString(R.string.delete_suc));
    }

    private void getTcpServerData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getTcpServerList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setList(listShow);
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.llDebugNoData.setVisibility(View.GONE);
            mDataBinding.tvAddConn.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.llDebugNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvAddConn.setVisibility(View.GONE);
        }
    }

    private void getTcpClientData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getTcpClientList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setList(listShow);
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.llDebugNoData.setVisibility(View.GONE);
            mDataBinding.tvAddConn.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.llDebugNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvAddConn.setVisibility(View.GONE);
        }
    }

    private void getUdpServerData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getUdpServerList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setList(listShow);
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.llDebugNoData.setVisibility(View.GONE);
            mDataBinding.tvAddConn.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.llDebugNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvAddConn.setVisibility(View.GONE);
        }
    }

    private void getUdpClientData() {
        if (listShow != null) {
            listShow.clear();
        }
        listShow = AppSpUtils.getUdpClientList();
        if (listShow != null && listShow.size() != 0) {
            debugAdapter.setList(listShow);
            mDataBinding.rvDebugList.setAdapter(debugAdapter);
            mDataBinding.rvDebugList.setVisibility(View.VISIBLE);
            mDataBinding.llDebugNoData.setVisibility(View.GONE);
            mDataBinding.tvAddConn.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvDebugList.setVisibility(View.GONE);
            mDataBinding.llDebugNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvAddConn.setVisibility(View.GONE);
        }
    }

    private void cancelEdit() {
        isEdit = false;
        debugAdapter.setEdit(isEdit);
        debugAdapter.notifyDataSetChanged();
        mDataBinding.tvDebugEdit.setVisibility(View.VISIBLE);
        mDataBinding.tvDebugComplete.setVisibility(View.GONE);
    }

    private void showDeleteDialog() {
        DeleteDialog dialog = new DeleteDialog(mContext);
        dialog.setListener(new DeleteDialog.Ilistener() {

            @Override
            public void clickDelete() {
                delete();
            }

        });
        dialog.show();
    }

}